<?php
class Carts_mdl extends CI_Model{
  //操作的表.
  const TAB_NAME='clickrcms_carts';

  function __construct(){
    parent::__construct();
    log_message('debug','sms_mdl Model class Initialized');
  }
  public function addProductToCard($product_id,$product_num,$spec_key=null,$attrs=null){
      $session_id = $this->nsession->userdata('session_id');
      $this->db->where('session_id',$session_id);
      $this->db->where('product_id',$product_id);
      if(!empty($spec_key)){
         $this->db->where('spec_key',$spec_key);
      }
      if($product_num <= 0){
         $row = $this->db->delete(self::TAB_NAME);
         return true;
      }
      $result = $this->db->get(self::TAB_NAME)->row_array();
      $attrs = !empty($attrs) ? serialize($attrs) : '';
      if($result){
         $this->db->where('session_id',$session_id);
         $this->db->where('product_id',$product_id);
         $this->db->update(self::TAB_NAME,array('num'=>$product_num,'attrs'=>$attrs,'create_time'=>time()));
         return ($this->db->affected_rows()>0)?true:false;
      }else{
          $this->db->insert(self::TAB_NAME,array('num'=>$product_num,'product_id'=>$product_id,'spec_key'=>$spec_key,'attrs'=>$attrs,'create_time'=>time(),'session_id'=>$session_id));
          return ($this->db->affected_rows()>0)?true:false;
      }
  }
  /*更新属性*/
  public function updateCartAttr($product_id,$spec_key=null,$attrs=null){
      $session_id = $this->nsession->userdata('session_id');
      $this->db->where('session_id',$session_id);
      $this->db->where('product_id',$product_id);
      if(!empty($spec_key)){
         $this->db->where('spec_key',$spec_key);
      }
      $attrs = !empty($attrs) ? serialize($attrs) : '';
      $this->db->update(self::TAB_NAME,array('attrs'=>$attrs,'create_time'=>time()));
       return ($this->db->affected_rows()>0) ? true : false;
  }
  /*获取购物车里面的数量*/
  public function getCartTotal(){
      $session_id = $this->nsession->userdata('session_id');
      $this->db->where('session_id',$session_id);
      $this->db->select_sum('num');
      $result = $this->db->get(self::TAB_NAME)->row_array();
      return (int)$result['num'];
  }
  public function getCartProduct(&$attrs = array()){
      $session_id = $this->nsession->userdata('session_id');
      $this->db->where('session_id',$session_id);
      $results = $this->db->get(self::TAB_NAME)->result_array();
      $base = array();
      if($results){
          foreach($results as $vo){
            if(!empty($vo['spec_key'])){
                $base[$vo['product_id']][$vo['spec_key']] = $vo['num'];
                $attrs[$vo['product_id']."_".$vo['spec_key']] = empty($vo['attrs']) ? '' : unserialize($vo['attrs']);
              }else{
                 $base[$vo['product_id']] = $vo['num'];
                 $attrs[$vo['product_id']] = empty($vo['attrs']) ? '' : unserialize($vo['attrs']);
              }
          }
      }
      return $base;
  }
  public function getCartItem($product_id,$spec_key=null){
      $session_id = $this->nsession->userdata('session_id');
      $this->db->where('session_id',$session_id);
      $this->db->where('product_id',$product_id);
      if(!empty($spec_key)){
         $this->db->where('spec_key',$spec_key);
      }
      return $result = $this->db->get(self::TAB_NAME)->row_array();

  }
  /*删除购物车的数据*/
  public function delMemberCarts(){
      $session_id = $this->nsession->userdata('session_id');
      $this->db->where('session_id',$session_id);
      $row = $this->db->delete(self::TAB_NAME);
      return true;
  }
}